<?
ini_set("display_errors",1);
include($_SERVER['DOCUMENT_ROOT'].'/includes/functions.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/portals/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/groups/database.php');

init_session();



include($_SERVER['DOCUMENT_ROOT'].'/portals/header.inc.php');


$ref = @parse_url($_SERVER['HTTP_REFERER']);

if (!empty($_SESSION['user_id']) && preg_match('/\w+\.portal\.geographs\.org$/',$ref['host'])) {

	$bits = explode('.',$ref['host']);
	$domain = $bits[0];

	$row = getRow("SELECT portal_id,title,user_id,domain FROM portal WHERE domain = ".dbQuote($domain)." AND user_id = {$_SESSION['user_id']}");

	if (!empty($row['portal_id'])) {
		$id = $row['portal_id'];
		print "<div class=\"banner\">";
		print "Your portal: <b>".he($row['title'])."</b><br/>";
		print "<small>&middot; <a href=\"http://".he($row['domain']).".portal.geographs.org/\">Goto</a> ";
		print "&middot; <a href=\"portal.php?id=$id\">Overview</a> ";
		print "&middot; <a href=\"filters.php?id=$id\">Edit <i>Britain and Ireland</i> Filters</a> ";
		print "&middot; </small></div>";		
	}

}


?>

<h3>Welcome to the Experimental Geograph Portal Section</h3>

<p>A portal is a collection of images forming a subsection of the Geograph Archive, which now has nearly 2 million images.</p>

<p>By creating these themed portals, we hope to make browsing easier, quicker and more fun.</p>

<?

$sql = array();
$sql['columns'] = '*';
$sql['tables'] = array();
$sql['tables'][] = "portal";
$sql['wheres'] = array();

$sql['wheres'][] = "status_id = 1";
$sql['wheres'][] = "public = 'Y'";

if (!empty($_GET['subject'])) {
	$sql['wheres'][] = "subject = ".dbQuote($_GET['subject']);
	$sql['order'] = "title";
} else {
	$sql['order'] = "portal_id DESC";
}


$query = sqlMakeQuery($sql);

$data = getAssoc($query);



if ($data) {
	print "<div class=\"interestBox\"><big>Public portals...</big> | <a href=\"map.php\">View on Map</a> | <a href=\"types.php?all=1\">By Type</a> | <a href=\"portals.php\">View as thumbnails</a> | <a href=\"recent.rss.php\" 
class=\"xml-rss\">RSS</a><br/>";
	print "<small><b>Subject:</b> ";
	$subjects = array(''=>'Any','Geography'=>'Geography','Nations'=>'Nations','Surveying'=>'Surveying');
	foreach ($subjects as $key => $name) {
		if($key)
			print " | ";
		if ($_GET['subject'] == $key) {
			print "<b>$name</b>";
		} else {
			print "<a href=\"?subject=$key\">$name</a>";
		}
	}
	print "</small></div>";
	print "<ul class=\"list\">";
	foreach ($data as $id => $c) {
		$count = count_active($id);
		
		if ($count) {
			print "<li>";
			print "<b><a href=\"http://".hec('domain').".portal.geographs.org/\" target=\"_blank\"";
			if (!empty($c['excerpt']))
				print " title=\"".hec('excerpt')."\"";
			print ">".hec('title')."</a></b>";
		
			print " [$count images]";
		
			if (!empty($_SESSION['user_id']) && $c['user_id'] == $_SESSION['user_id']) {
				print "&middot; <a href=\"portal.php?id=$id\" class=\"edit\">Overview</a>";
				print "&middot; <a href=\"filters.php?id=$id\" class=\"edit\">Filters</a> ";

			}
			print "</li>";
		}
	}
	print "</ul>";
	
	#print "<p><a href=\"edit.php\">Create your own</a>!</p>";
	print "</div>";
} else {
	print "<p>No Portals to show at this time. <a href=\"edit.php\">Create one</a>!</p>";
}


include($_SERVER['DOCUMENT_ROOT'].'/portals/footer.inc.php');
